/**
 * [Numberic is defined as]:
 *     `parseFloat(val) == val`
 * For example:
 * numeric:
 *     typeof number except NaN, '-123', '123', '2e3', '-2e3', '011', 'Infinity', Infinity,
 *     and they rounded by white-spaces or line-terminal like ' -123 \n ' (see es spec)
 * not-numeric:
 *     null, undefined, [], {}, true, false, 'NaN', NaN, '123ab',
 *     empty string, string with only white-spaces or line-terminal (see es spec),
 *     0x12, '0x12', '-0x12', 012, '012', '-012',
 *     non-string, ...
 *
 * @test See full test cases in `test/ut/spec/util/number.js`.
 * @return Must be a typeof number. If not numeric, return NaN.
 */
export function numericToNumber(val: any) {
  const valFloat = parseFloat(val);
  return valFloat == val && // eslint-disable-line eqeqeq
    (valFloat !== 0 || typeof val !== 'string' || val.indexOf('x') <= 0) // For case ' 0x0 '.
    ? valFloat
    : NaN;
}

/**
 * Definition of "numeric": see `numericToNumber`.
 */

export function isNumeric(val: any) {
  return !isNaN(numericToNumber(val));
}

/** 判断是否是百分数 */
export function isPercent(val: any) {
  return String(val).slice(-1) === '%';
}
